package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.user.UserModel;
import com.park.client.model.user.UserMsgModel;
import com.park.client.service.user.UserMsgService;
import com.park.client.service.user.UserService;
import com.park.common.constant.Constant;
import com.park.common.util.MD5;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.util.SessionUtil;

@Controller
@RequestMapping("/admin")
public class AdminUserController extends BaseController{
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserMsgService userMsgService;
	
	@RequestMapping("login")
	public String login(HttpServletRequest request){
		return Global.ADMIN_PATH+"login";
	}
	
	@RequestMapping("userList")
	public String userList(HttpServletRequest request){
			return Global.ADMIN_PATH+"user/userList";
	}
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<UserModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<UserModel> pageData = new PageData<>();
		UserModel user = new UserModel();
		Integer total = userService.selectTotal(user);
		Page page = new Page(pageNow,total,rows);
		user.setStartPos(page.getStartPos());
		user.setPageSize(page.getPageSize());
		List<UserModel> list = userService.selectPagination(user);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	/**
	 * 删除用户
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(Long id,HttpServletRequest request){
		Result<String> result = new Result<>(true);
		boolean success = userService.deleteByPrimaryKey(id);
		if(success){
			result.setMessage("删除成功!");
		}else{
			result.setSuccess(false);
			result.setMessage("删除失败!");
		}
			return result;
	}
	
	/**
	 * 添加用户
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result<String> save(UserModel user,HttpServletRequest request){
			Result<String> json = new Result<>();
			boolean success = userService.insertSelective(user);
			if(success){
				json.setSuccess(true);
				json.setMessage("添加成功!");
			}else{
				json.setSuccess(false);
				json.setMessage("添加失败!");
			}
			return json;
	}
	
	/**
	 * 修改用户
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(UserModel user,HttpServletRequest request){
		Result<String> json = new Result<String>();
		boolean success = userService.updateByPrimaryKeySelective(user);
		if(success){
			json.setSuccess(true);
			json.setMessage("修改成功!");
		}else{
			json.setSuccess(false);
			json.setMessage("修改失败!");
		}
		return json;
	}
	
	/**
	 * 充值确认，暂时这样写
	 */
	@RequestMapping("depositConfirm")
	@ResponseBody
	public Result<String> depositConfirm(UserModel user,HttpServletRequest request,HttpSession session){
		Result<String> result = new Result<>();
		user = userService.selectByPrimaryKey(user.getId());
		user.setIntegral(user.getIntegral()+Constant.INTEGRAL);
		boolean success = userService.updateByPrimaryKeySelective(user);
		if(success){
			result.setMessage(Constant.SUCCESS_MSG);
			//发送站内消息
			UserMsgModel msg = new UserMsgModel();
			msg.setMsgTitle(Constant.SUCCESS_DEPOSIT_TITLE);
			msg.setMsgContent(Constant.SUCCESS_DEPOSIT_INTEGRAL);
			msg.setCreater(SessionUtil.getUserID(session));
			userMsgService.saveMsg(msg, user.getId());
		}else{
			result.setSuccess(false);
			result.setMessage(Constant.FAIL_MSG);
		}
		return result;
	}
	
	/**
	 *冻结帐户
	 */
	@RequestMapping("freezeAccount")
	@ResponseBody
	public Result<String> freezeAccount(@RequestParam("ids")String ids){
		Result<String> json = new Result<>();
		try {
			for (String id : ids.split(",")) {
				  userService.updateAccountStatus(id);
			}
			json.setSuccess(true);
			json.setMessage("账号成功冻结!");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMessage("操作出现异常!");
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 登录验证
	 **/
	@RequestMapping("loginCheck")
	@ResponseBody//将对象转化为json对象
	public Result<UserModel> loginCheck(UserModel user ,HttpServletRequest request,HttpSession session){
			Result<UserModel> json = new Result<>();
			UserModel u= userService.login(user.getUsername());
			String password = MD5.md5(user.getPassword());
			boolean success = false ;
			if(u != null){
				if(u.getPassword().equals(password)){
					if(1== u.getIsAdmin()){//非管理员不允许进后台
						String msg = "";
						switch(u.getDisabled()){
			       			 	case -1:
			       			 		msg = "账号未认证!";
			       			 		break;
			       			 	case 0:
			       			 		msg = "账号已禁用!";
			       			 		break;
			       			 	case 1:
			       			 		success = true;
			       			 		msg = "登陆成功!";
			       			 		session.setAttribute("user", u);
			       			 		break;
							}
						json.setSuccess(success);
						json.setMessage(msg);
						json.setObj(u);
					}else{
						json.setMessage("你不是管理员,不允许进入管理有台!");
						json.setSuccess(success);
					}
				}else{
					json.setMessage("密码错误!");
					json.setSuccess(success);
				}
			}else{
				json.setMessage("用户名不存在!");
				json.setSuccess(success);
			}
			return json;
	}
	
	@RequestMapping("logout")
	public String logout(HttpSession httpSession,HttpServletRequest request){
		UserModel user = (UserModel) httpSession.getAttribute("user");
		if(user != null){
			httpSession.invalidate();
		}
			return  "redirect:login";
	}
	
	
	@RequestMapping("index")
	public String index(UserModel user,HttpServletRequest request){
		return Global.ADMIN_PATH+"index";
	}
	
	/**
	 * 用户分配角色
	 */
	@RequestMapping("assignRole")
	@ResponseBody
	public Result<String> assignRole(UserModel user,HttpServletRequest request){
		Result<String> result = new Result<>();
		return result;
	}
	
}
